package com.woniuxy.education.utils;

import com.woniuxy.education.entity.*;
import com.woniuxy.education.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 根据题目id查出题目
 * 方法一 用于题库查询,主要是有判断是否是错题
 * 方法二 用于收藏的题目查询
 */
@Component
public class SelectTitleUtils {
    private static SelectTitleUtils selectTitleUtils;
    @Autowired
    private StudentMapper studentMapper;
    @PostConstruct
    public void init(){
        selectTitleUtils = this;
    }

    public static List<Allquestions> Allques(String str,int id,List<Allquestions> allquestions,String iserror){
        if(str.length()==0){
            return allquestions;
        }
        String[] string = str.split(",");
        String[] error = iserror.split(",");
        List<String> strArr = new ArrayList<>(Arrays.asList(error));
        boolean fault=false;
        //如果是选择题
        if(id==1){
            for (int i=0;i<string.length;i++){
                Choose_table choose_table = selectTitleUtils.studentMapper.findChoose(Integer.parseInt(string[i]));
                String optionsStr =
                        choose_table.getTitle()+" "+
                                "A:"+choose_table.getA_option()+" " +
                                "B:"+choose_table.getB_option()+" "+
                                "C:"+choose_table.getC_option()+" "+
                                "D:"+choose_table.getD_option();
                Allquestions allchoose=new Allquestions();
                allchoose.setTitleid(choose_table.getId());
                allchoose.setQuestion(optionsStr);
                allchoose.setAnswer(choose_table.getAnswer());
                allchoose.setSubject(choose_table.getSubject());
                allchoose.setTitleType(1);
                for (String item : strArr) {
                    if (item.equals(allchoose.getTitleid())||Integer.parseInt(item)==allchoose.getTitleid()) {
                        strArr.remove(item);
                        fault = true;
                        break;
                    }
                }
                if(fault==true){
                    allchoose.setIserror("是");
                }else {
                    allchoose.setIserror("否");
                }
                allquestions.add(allchoose);
                fault=false;
            }
            return allquestions;
        }
        //如果是填空题
        if(id==2){
            for (int i=0;i<string.length;i++){
                Completion_table completion_table = selectTitleUtils.studentMapper.findCompletion(Integer.parseInt(string[i]));
                Allquestions allcompletion=new Allquestions();
                allcompletion.setTitleid(completion_table.getId());
                allcompletion.setQuestion(completion_table.getTitle());
                allcompletion.setAnswer(completion_table.getAnswer());
                allcompletion.setSubject(completion_table.getSubject());
                allcompletion.setTitleType(2);
                for (String item : strArr) {
                    if (item.equals(allcompletion.getTitleid())||Integer.parseInt(item)==allcompletion.getTitleid()) {
                        strArr.remove(item);
                        fault = true;
                        break;
                    }
                }
                if(fault==true){
                    allcompletion.setIserror("是");
                }else {
                    allcompletion.setIserror("否");
                }
                allquestions.add(allcompletion);
                fault=false;
            }
            return allquestions;
        }
        //如果是解答题
        if(id==3){
            for (int i=0;i<string.length;i++){
                Answer_Questions_table answerquestions_table = selectTitleUtils.studentMapper.findAnswer_Questions(Integer.parseInt(string[i]));
                Allquestions allanswerquestions=new Allquestions();
                allanswerquestions.setTitleid(answerquestions_table.getId());
                allanswerquestions.setQuestion(answerquestions_table.getTitle());
                allanswerquestions.setAnswer(answerquestions_table.getAnswer());
                allanswerquestions.setSubject(answerquestions_table.getSubject());
                allanswerquestions.setTitleType(3);
                for (String item : strArr) {
                    if (item.equals(allanswerquestions.getTitleid())||Integer.parseInt(item)==allanswerquestions.getTitleid()) {
                        strArr.remove(item);
                        fault = true;
                        break;
                    }
                }
                if(fault==true){
                    allanswerquestions.setIserror("是");
                }else {
                    allanswerquestions.setIserror("否");
                }
                allquestions.add(allanswerquestions);
                fault=false;
            }
            return allquestions;
        }
        //如果是作文题
        if(id==4){
            for (int i=0;i<string.length;i++){
                Composition_table composition_table = selectTitleUtils.studentMapper.findComposition(Integer.parseInt(string[i]));
                Allquestions allcomposition=new Allquestions();
                allcomposition.setTitleid(composition_table.getId());
                allcomposition.setQuestion(composition_table.getTitle());
                allcomposition.setSubject(composition_table.getSubject());
                allcomposition.setTitleType(4);
                for (String item : strArr) {
                    if (item.equals(allcomposition.getTitleid())||Integer.parseInt(item)==allcomposition.getTitleid()) {
                        strArr.remove(item);
                        fault = true;
                        break;
                    }
                }
                if(fault==true){
                    allcomposition.setIserror("是");
                }else {
                    allcomposition.setIserror("否");
                }
                allquestions.add(allcomposition);
                fault=false;
            }
            return allquestions;
        }
        return null;
    }

    /**
     * 方法二
     * @param str
     * @param id
     * @param allquestions
     * @return
     */
    public static List<Allquestions> Allques(String str,int id,List<Allquestions> allquestions){
        if(str.length()==0){
            return allquestions;
        }
        String[] string = str.split(",");
        boolean fault=false;
        //如果是选择题
        if(id==1){
            for (int i=0;i<string.length;i++){
                Choose_table choose_table = selectTitleUtils.studentMapper.findChoose(Integer.parseInt(string[i]));
                String optionsStr =
                        choose_table.getTitle()+"\n"+
                                "A:"+choose_table.getA_option()+"\n" +
                                "B:"+choose_table.getB_option()+"\n"+
                                "C:"+choose_table.getC_option()+"\n"+
                                "D:"+choose_table.getD_option();
                Allquestions allchoose=new Allquestions();
                allchoose.setTitleid(choose_table.getId());
                allchoose.setQuestion(optionsStr);
                allchoose.setAnswer(choose_table.getAnswer());
                allchoose.setSubject(choose_table.getSubject());
                allchoose.setTitleType(1);
                allquestions.add(allchoose);
            }
            return allquestions;
        }
        //如果是填空题
        if(id==2){
            for (int i=0;i<string.length;i++){
                Completion_table completion_table = selectTitleUtils.studentMapper.findCompletion(Integer.parseInt(string[i]));
                Allquestions allcompletion=new Allquestions();
                allcompletion.setTitleid(completion_table.getId());
                allcompletion.setQuestion(completion_table.getTitle());
                allcompletion.setAnswer(completion_table.getAnswer());
                allcompletion.setSubject(completion_table.getSubject());
                allcompletion.setTitleType(2);
                allquestions.add(allcompletion);
            }
            return allquestions;
        }
        //如果是解答题
        if(id==3){
            for (int i=0;i<string.length;i++){
                Answer_Questions_table answerquestions_table = selectTitleUtils.studentMapper.findAnswer_Questions(Integer.parseInt(string[i]));
                Allquestions allanswerquestions=new Allquestions();
                allanswerquestions.setTitleid(answerquestions_table.getId());
                allanswerquestions.setQuestion(answerquestions_table.getTitle());
                allanswerquestions.setAnswer(answerquestions_table.getAnswer());
                allanswerquestions.setSubject(answerquestions_table.getSubject());
                allanswerquestions.setTitleType(3);
                allquestions.add(allanswerquestions);
            }
            return allquestions;
        }
        //如果是作文题
        if(id==4){
            for (int i=0;i<string.length;i++){
                Composition_table composition_table = selectTitleUtils.studentMapper.findComposition(Integer.parseInt(string[i]));
                Allquestions allcomposition=new Allquestions();
                allcomposition.setTitleid(composition_table.getId());
                allcomposition.setQuestion(composition_table.getTitle());
                allcomposition.setSubject(composition_table.getSubject());
                allcomposition.setTitleType(4);
                allquestions.add(allcomposition);
            }
            return allquestions;
        }
        return null;
    }
}
